package com.dnzx.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.dnzx.framework.page.PageResult;
import org.dnzx.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dnzx.dao.IPaymentDao;
import com.dnzx.model.MemberBO;
import com.dnzx.model.PaymentBO;
import com.dnzx.service.IMemberService;
import com.dnzx.service.IParamService;
import com.dnzx.service.IPaymentService;

/**
 * 投资信息
 */
@Service
public class PaymentServiceImpl implements IPaymentService {

    @Resource
    private IPaymentDao paymentDao;
    @Autowired
    private IMemberService memberService;
    @Autowired
    private IParamService paramService;
    
    @Override
    public void save(PaymentBO paymentBO) throws Exception {
//    	// 校验项目是否存在，状态是否进行中，金额是否超过（是否需要锁定才行） TODO
//    	ProjectBO projectBO = this.projectService.get(paymentBO.getProjectId());
//    	if (projectBO == null || !"1".equals(projectBO.getStatus())) {// 项目不存在，或者项目已完成，则不可投入
//    		throw new Exception("项目不存在或者项目已完成，不可投入。");
//    	}
//    	// 校验起投金额
//    	if (paymentBO.getAmt() < projectBO.getMinAmt()) {
//    		throw new Exception("投入金额不可低于起投金额，请重新输入投入金额");
//    	}
//    	
//    	double availableAmt = projectBO.getAvailableAmt() + paymentBO.getAmt();
//    	if (availableAmt > projectBO.getTotalAmt()) {// 项目可投金额超出
//    		throw new Exception("投入金额已经超过项目可投金额，请重新输入投入金额");
//    	}
//    	
//    	MemberBO memberBO = this.memberService.getLoginMemberBO();
//    	double balanceAmt = memberBO.getBalanceAmt() - paymentBO.getAmt();
//    	if (balanceAmt < memberBO.getFreezeBalanceAmt()) {
//    		throw new Exception("您的账号余额不足，请充值后再进行投入。");
//    	}
    	
    	String sn = "PAY_" + DateUtil.getTimeSss();
    	paymentBO.setSn(sn);
//    	paymentBO.setBalanceAmt(balanceAmt);
//    	paymentBO.setAddUser(memberBO.getId());
//    	paymentBO.setUserId(memberBO.getId());
//    	paymentBO.setProjectAmt(availableAmt);
    	paymentBO.setTradeTime(new Date());
    	paymentBO.setPayType("1");
        this.paymentDao.save(paymentBO);
        
//        // 投资成功后，更新项目Project信息 
//        projectBO.setAvailableAmt(availableAmt);
//        this.projectService.update(projectBO);
//        
//        // 投资成功后，更新会员Member信息 
//        memberBO.setBalanceAmt(balanceAmt);
//        this.memberService.update(memberBO);
//        
//        // 保存余额变化 
//		BalanceLogBO balanceLogBO = new BalanceLogBO();
//		balanceLogBO.setAmt(paymentBO.getAmt());
//    	balanceLogBO.setBalance(balanceAmt);// 收益后用户余额
//    	balanceLogBO.setSn(sn);
//    	balanceLogBO.setTradeTime(new Date());
//    	balanceLogBO.setType("3");// 提现
//    	balanceLogBO.setUserId(paymentBO.getUserId());
//    	balanceLogBO.setContent("项目投入");
//    	balanceLogBO.setAddUser(paymentBO.getUserId());// 直接算自己创建
//    	this.balanceLogService.save(balanceLogBO);
        
        // TODO 保存贡献分BO（自己、分享人以及10级分享人的贡献分）
    	this.calculateDevote(paymentBO);
    }
    
    public void calculateDevote(PaymentBO paymentBO) throws Exception {
    	// 获取本级的系数
//    	double usdtDevoteRate = new Double(this.paramService.get("USDT_DEVOTE_RATE"));// USDT和贡献分转换系数
//    	double mark = paymentBO.getAmt() / usdtDevoteRate;
//    	DevoteBO devoteBO = new DevoteBO();
//    	devoteBO.setAmt(paymentBO.getAmt());// 投入金额
//    	devoteBO.setDevoteId(paymentBO.getUserId());// 贡献人
//    	devoteBO.setLevel("0");
//    	devoteBO.setMark(mark);
//    	devoteBO.setProjectId(paymentBO.getProjectId());
//    	devoteBO.setTradeTime(new Date());
//    	devoteBO.setUserId(paymentBO.getUserId());// 受益人
//        this.devoteService.save(devoteBO);
//        
//        MemberBO memberBO = this.memberService.get(paymentBO.getUserId());
//        double subDevoteRate = new Double(this.paramService.get("SUB_DEVOTE_RATE"));// 下级贡献分比例
//        int devoteLevel = new Integer(this.paramService.get("DEVOTE_LEVEL"));// 可贡献层级
        
        // 计算分享人贡献分（往上10级） ,需要注意的是，事务是否有同步，以及并发的情况 TODO 
//        this.calculateDevoteShare(paymentBO, memberBO, 1 , subDevoteRate, devoteLevel, usdtDevoteRate);
    }
    
    /*
     * 计算分享人贡献分
     */
    private void calculateDevoteShare(PaymentBO paymentBO, MemberBO memberBO, 
    		Integer n, double subDevoteRate, int devoteLevel, double usdtDevoteRate) throws Exception {
//    	if (memberBO.getShareId() != null && memberBO.getShareId() > 0 && n <= devoteLevel) {
//    		double mark = paymentBO.getAmt() * Math.pow(subDevoteRate,n) / usdtDevoteRate;
//        	MemberBO memberBOShare = this.memberService.get(memberBO.getShareId());
//        	DevoteBO devoteBO = new DevoteBO();
//        	devoteBO.setAmt(paymentBO.getAmt());// 投入金额
//        	devoteBO.setDevoteId(paymentBO.getUserId());// 贡献人
//        	devoteBO.setLevel(String.valueOf(n));
//        	devoteBO.setMark(mark);
//        	devoteBO.setProjectId(paymentBO.getProjectId());
//        	devoteBO.setTradeTime(new Date());
//        	devoteBO.setUserId(memberBO.getId());// 受益人
//            this.devoteService.save(devoteBO);
//        	n++;
//        	this.calculateDevoteShare(paymentBO, memberBOShare, n , subDevoteRate, devoteLevel, usdtDevoteRate);
//    	}
    }

    @Override
    public void delete(Long id) throws Exception {
        this.paymentDao.delete(id);
    }

    @Override
    public void update(PaymentBO paymentBO) throws Exception {
        this.paymentDao.update(paymentBO);
    }

    @Override
    public PaymentBO get(Long id) {
        return this.paymentDao.get(id);
    }
    
    @Override
	public PaymentBO get(PaymentBO paymentBO) {
		// TODO Auto-generated method stub
		return null;
	}

    @Override
    public List<PaymentBO> queryList(PaymentBO paymentBO) {
        return this.paymentDao.queryList(paymentBO);
    }

    @Override
    public PageResult<List<PaymentBO>> queryPage(PaymentBO paymentBO, PageResult<List<PaymentBO>> pageResult) {
    	int totalRows = this.paymentDao.queryCount(paymentBO);
        pageResult.setTotalRows(totalRows);
        pageResult.setCurrentPage(paymentBO.getCurrentPage());
        pageResult.setPageSize(paymentBO.getPageSize());
		List<PaymentBO> records = this.paymentDao.queryPage(pageResult.getPageSize(), pageResult.getStartRow(), paymentBO);
		pageResult.setT(records);
		return pageResult;
    }

}
